/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * OrderNewUI.java
 *
 * Created on Jan 11, 2011, 10:57:04 AM
 */

package GUILayer;

import ControllerLayer.*;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import ModelLayer.*;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


/**
 *
 * @author pelle
 */
public class SalesLine extends javax.swing.JFrame {
    
    private SalesOrderController orderCtr = SalesOrderController.getInstance();
    private PersonController personsCtr = PersonController.getInstance();
    ArrayList<SalesLineItem> items = null;
    private SalesOrder order;
    JFrame frame;

    private boolean createNewOrder = true;


    /** Creates new form OrderNewUI */
    public SalesLine() {
        initComponents();
        initValues();
    }

//    public SalesLine(int orderId) {
//        initComponents();
//
//        if(orderCtr.getOrder(orderId) != null) {
//            updateDetails();
//        } else {
//            JOptionPane.showMessageDialog(frame, "Ordren blev ikke fundet!", "Ikke fundet!", JOptionPane.WARNING_MESSAGE);
//            dispose();
//        }
//
//        createNewOrder = false;
//        changeLabelsToEdit();
//        initValues();
//    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        headline = new javax.swing.JLabel();
        jPanel1 = new javax.swing.JPanel();
        jLabel5 = new javax.swing.JLabel();
        itemBarcode = new javax.swing.JTextField();
        jButton2 = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        itemsTable = new javax.swing.JTable();
        removeItem = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        customerName = new javax.swing.JLabel();
        customerAddress = new javax.swing.JLabel();
        customerZipcodeCity = new javax.swing.JLabel();
        customerPhone = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        subTotal = new javax.swing.JLabel();
        moms = new javax.swing.JLabel();
        total = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jLabel11 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        orderId = new javax.swing.JLabel();
        deleteOrdre = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        headline.setFont(new java.awt.Font("Lucida Grande", 0, 24));
        headline.setText("Ny ordre");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Varer"));

        jLabel5.setText("Stregkode:");

        itemBarcode.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                itemBarcodeKeyReleased(evt);
            }
        });

        jButton2.setText("»");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        itemsTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null}
            },
            new String [] {
                "Antal", "Titel", "á", "Subtotal"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Float.class, java.lang.Float.class
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }
        });
        jScrollPane2.setViewportView(itemsTable);
        itemsTable.getColumnModel().getColumn(0).setResizable(false);
        itemsTable.getColumnModel().getColumn(1).setResizable(false);
        itemsTable.getColumnModel().getColumn(2).setResizable(false);
        itemsTable.getColumnModel().getColumn(3).setResizable(false);

        removeItem.setText("Fjern");
        removeItem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeItemActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 500, Short.MAX_VALUE)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel5)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(itemBarcode, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 315, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 47, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(removeItem, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 65, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 181, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(itemBarcode, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel5)
                    .add(removeItem)
                    .add(jButton2)))
        );

        jButton3.setText("Opret ordre");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText("Annuller");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Kunde"));

        customerName.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        customerName.setText("<Ingen kunde valgt>");

        customerAddress.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        customerAddress.setText(" ");

        customerZipcodeCity.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        customerZipcodeCity.setText(" ");

        customerPhone.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        customerPhone.setText(" ");

        jButton1.setText("Vælg kunde");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jButton1)
                    .add(customerName)
                    .add(customerAddress)
                    .add(customerZipcodeCity)
                    .add(customerPhone))
                .addContainerGap(128, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(customerName)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(customerAddress)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(customerZipcodeCity)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(customerPhone)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jButton1))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Ordre detaljer"));

        jLabel2.setFont(new java.awt.Font("Lucida Grande", 1, 12));
        jLabel2.setText("Subtotal:");

        jLabel3.setFont(new java.awt.Font("Lucida Grande", 0, 12));
        jLabel3.setText("Moms (25%):");

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 1, 12));
        jLabel4.setText("Total:");

        subTotal.setText("0");

        moms.setText("0");

        total.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        total.setText("0");

        jLabel9.setText("kr.");

        jLabel10.setText("kr.");

        jLabel11.setFont(new java.awt.Font("Lucida Grande", 1, 13));
        jLabel11.setText("kr.");

        jLabel6.setText("Ordre nr.:");

        orderId.setText("0");

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel3Layout.createSequentialGroup()
                        .add(jLabel6)
                        .add(26, 26, 26)
                        .add(orderId))
                    .add(jPanel3Layout.createSequentialGroup()
                        .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel2)
                            .add(jLabel3)
                            .add(jLabel4))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jPanel3Layout.createSequentialGroup()
                                .add(total)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel11))
                            .add(jPanel3Layout.createSequentialGroup()
                                .add(moms)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel10))
                            .add(jPanel3Layout.createSequentialGroup()
                                .add(subTotal)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jLabel9)))))
                .addContainerGap(132, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(orderId))
                .add(18, 18, 18)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(subTotal)
                    .add(jLabel9))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel3Layout.createSequentialGroup()
                        .add(jLabel3)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel4)
                            .add(total)
                            .add(jLabel11)))
                    .add(jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(moms)
                        .add(jLabel10)))
                .addContainerGap(21, Short.MAX_VALUE))
        );

        deleteOrdre.setText("Slet ordre");
        deleteOrdre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteOrdreActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(deleteOrdre)
                            .add(headline))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 274, Short.MAX_VALUE)
                        .add(jButton4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jButton3))
                    .add(layout.createSequentialGroup()
                        .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(headline)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 254, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel3, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jButton3)
                    .add(jButton4)
                    .add(deleteOrdre))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void initValues() {
        order = new SalesOrder();
        order.setDate("1");
        order.setDeliveryDate("1");
        order.setDiscount(0);
    }
//getDateTime()
    private void changeLabelsToEdit() {
        headline.setText("Ret ordre");
        jButton3.setText("Gem ændringer");
    }

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        addItem();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void itemBarcodeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_itemBarcodeKeyReleased
        if(evt.getKeyCode() == 10) {
            addItem();
        }
    }//GEN-LAST:event_itemBarcodeKeyReleased

    private String getDateTime() {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
        Date date = new Date();
        return dateFormat.format(date);
    }

    private void addItem() {

        String text = this.itemBarcode.getText();
        if(text.length() > 0) {
            try {
                int barcode = Integer.valueOf(text);
                orderCtr.addItemToOrder(order,barcode);

                updateDetails();
                this.itemBarcode.setText("");
            } catch(Exception e) {
                JOptionPane.showMessageDialog(frame, "Stegkoder må kun indeholde tal fra 0-9", "Advarsel", JOptionPane.WARNING_MESSAGE);
                this.itemBarcode.setText(text.substring(0, text.length()-1));
            }
        }
    }

    private void updateDetails() {
        items = order.getSalesLineItems();
        

        Object quantity = "Antal";
        Object title = "Titel";
        Object price = "Á";
        Object subtotal = "Subtotal";

        Object obj[] = null;

        DefaultTableModel listModel = new DefaultTableModel();

        listModel.addColumn(quantity);
        listModel.addColumn(title);
        listModel.addColumn(price);
        listModel.addColumn(subtotal);

        if(items.size() > 0) {
            for(SalesLineItem sli : items) {
                Product item = sli.getProduct();

                obj = new Object [] { sli.getQuantity(), item.getName(), item.getSalesPrice(), sli.getLineTotal() };
                listModel.addRow(obj);
            }
        }
        
        itemsTable.removeAll();
        itemsTable.setModel(listModel);

        double p = order.getAmount();
        double vat = p*0.25;

        orderId.setText("" + order.getSalesOrderID());
        subTotal.setText("" + p);
        moms.setText("" + vat);
        total.setText("" + (p + vat));
    }

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        new CustomerSearchUI(order).setVisible(true);
        addCustomer(order.getCustomer());
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton4ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        String error = null;

        if(order.getTotalSaleLines() != null)
        {
            
            if(order.getCustomer() != null)
            {
                 if(createNewOrder)
                 {

                     orderCtr.createOrder(order);
                
            }
             else
             {
                error = "Ingen kunde valgt!";
             }
        }
        else
        {
            error = "Ingen varer tilføjet!";
        }

        if(error != null && error.length() > 0) {
            JOptionPane.showMessageDialog(frame, error, "Fejl!", JOptionPane.ERROR_MESSAGE);
        }
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void deleteOrdreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteOrdreActionPerformed

    }//GEN-LAST:event_deleteOrdreActionPerformed

    private void removeItemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeItemActionPerformed
        int selectedIndex = itemsTable.getSelectedRow();
        if(selectedIndex >= 0) {
            SalesLineItem sli = items.get(selectedIndex);
            if(sli != null) {
                orderCtr.deleteOrder(selectedIndex);
            }
            updateDetails();
        }
    }//GEN-LAST:event_removeItemActionPerformed


    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new SalesLine().setVisible(true);
            }
        });
    }

        
    

    public void addCustomer(Customer customer) {
        customerName.setText(customer.getName() + " (ID: " + customer.getPersonID() + ")");
        customerAddress.setText(customer.getAddress());
        customerZipcodeCity.setText(customer.getZipCode() + " " + customer.getCity());
        customerPhone.setText(customer.getPhoneNo());
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel customerAddress;
    private javax.swing.JLabel customerName;
    private javax.swing.JLabel customerPhone;
    private javax.swing.JLabel customerZipcodeCity;
    private javax.swing.JButton deleteOrdre;
    private javax.swing.JLabel headline;
    private javax.swing.JTextField itemBarcode;
    private javax.swing.JTable itemsTable;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JLabel moms;
    private javax.swing.JLabel orderId;
    private javax.swing.JButton removeItem;
    private javax.swing.JLabel subTotal;
    private javax.swing.JLabel total;
    // End of variables declaration//GEN-END:variables

}
